<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录页面</title>
    <style>
        body {
            font-family: '楷体', Arial, sans-serif;
            background-color: #f0f0f0;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
        form {
            font-family: '楷体', Arial, sans-serif;
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
            width: 300px;
        }
        input[type="text"],
        input[type="password"],
        input[type="submit"] {
            width: 100%;
            padding: 10px;
            margin: 5px 0;
            box-sizing: border-box;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        input[type="submit"] {
            background: linear-gradient(to right, #4CAF50, #2196F3);
            color: white;
            cursor: pointer;
            transition: background 0.3s;
        }
        input[type="submit"]:hover {
            background: linear-gradient(to right, #2196F3, #4CAF50);
        }
    </style>
</head>
<body>
    <form id="loginForm" onsubmit="checkLogin(event)">
        <h2 style="text-align: center;">用户登录</h2>
        <label for="username">用户名：</label><br>
        <input type="text" id="username" name="username" required><br>
        <label for="password">密码：</label><br>
        <input type="password" id="password" name="password" required><br>
        <input type="submit" value="登录">
        <p id="errorMessage" style="color: red; display: none;"></p>
    </form>

    <script>
        function checkLogin(event) {
            event.preventDefault();
            var username = document.getElementById('username').value;
            var password = document.getElementById('password').value;

            fetch('/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: new URLSearchParams({ username: username, password: password }).toString(),
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    window.location.href = '/yxh';
                } else {
                    document.getElementById('errorMessage').innerText = "登录失败，请检查用户名和密码";
                    document.getElementById('errorMessage').style.display = "block";
                }
            });
        }
    </script>    
</body>
</html>
